﻿using System;
using System.Windows.Forms;

namespace DbSharp.UIControls.Enhancement
{
    
    class TabControlHelper
    {
        private TabControl m_tabControl;

        public TabControlHelper(TabControl tabCtrl)
        {
            this.m_tabControl = tabCtrl;
        }

        public void HideTabPage(TabPage tp)
        {
            if (this.m_tabControl.TabPages.Contains(tp))
            {
                this.m_tabControl.TabPages.Remove(tp);
            }
        }

        private void InsertTabPage(TabPage tabpage, int index)
        {
            if ((index < 0) || (index > this.m_tabControl.TabCount))
            {
                throw new ArgumentException("Index out of Range.");
            }
            this.m_tabControl.TabPages.Add(tabpage);
            if (index < (this.m_tabControl.TabCount - 1))
            {
                do
                {
                    this.SwapTabPages(tabpage, this.m_tabControl.TabPages[this.m_tabControl.TabPages.IndexOf(tabpage) - 1]);
                }
                while (this.m_tabControl.TabPages.IndexOf(tabpage) != index);
            }
            this.m_tabControl.SelectedTab = tabpage;
        }

        public void ShowTabPage(TabPage tp)
        {
            this.ShowTabPage(tp, this.m_tabControl.TabPages.Count);
        }

        public void ShowTabPage(TabPage tp, int index)
        {
            if (!this.m_tabControl.TabPages.Contains(tp))
            {
                this.InsertTabPage(tp, index);
            }
        }

        private void SwapTabPages(TabPage tp1, TabPage tp2)
        {
            if (!(this.m_tabControl.TabPages.Contains(tp1) && this.m_tabControl.TabPages.Contains(tp2)))
            {
                throw new ArgumentException("TabPages must be in the TabControls TabPageCollection.");
            }
            int index = this.m_tabControl.TabPages.IndexOf(tp1);
            int num2 = this.m_tabControl.TabPages.IndexOf(tp2);
            this.m_tabControl.TabPages[index] = tp2;
            this.m_tabControl.TabPages[num2] = tp1;
            this.m_tabControl.SelectedIndex = this.m_tabControl.SelectedIndex;
            string text = tp1.Text;
            string str2 = tp2.Text;
            tp1.Text = str2;
            tp2.Text = text;
        }
    }
}

